package com.fatalsoftware.java.browserbee;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil 
{
	private String s;
	private String d;
	private String destPath;

	public FileUtil() 
	{
		this.s = "";
		this.d = "";
		this.destPath = "C:/";
	}
	
	public FileUtil(String sourceFile, String destinationFile, String destinationPath) 
	{
		this.s = sourceFile;
		this.d = destinationFile;
		this.destPath = destinationPath;
	}


	public String getSourceFile() {
		return s;
	}


	public void setSourceFile( String sourceFile ) {
		this.s = sourceFile;
	}


	public String getDestinationFile() {
		return d;
	}

	public void setDestinationFile( String destinationFile ) {
		this.d = destinationFile;
	}
	
	public void setDestinationPath( String destinationPath ) 
	{
		this.destPath = destinationPath;
	}
	
	public String getDestinationPath() 
	{
		return destPath;
	}
	
	public void copyFile() 
	{
		if ( areFilesInitialized() )
			this.copyFile(this.s, this.destPath, this.d);
		else if ( isSourceFileInitialized() )
			System.out.println("Error: Source file is not initialized.");
		else
			System.out.println("Error: Destination file is not initialized.");
	}
	
	public void copyFile( String sourceFile, String destinationPath, String destinationFile )
	{
		try {
			InputStream in = new FileInputStream( sourceFile );
	
			//For Overwrite the file.
			OutputStream out = new FileOutputStream( destinationPath + destinationFile );
	
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException fnfe) {
			System.out.println(fnfe.getMessage() + " in the specified directory.");
			System.exit(0);
		} catch(IOException ioe) {
			System.out.println(ioe.getMessage());  
		}
	}
	
	public void setDefaultIEDestination()
	{
		setDestinationPath( System.getProperty("user.home")+ "/Favorites/" );
	}
	
	private Boolean isDestinationFileInitialized()
	{
		if (this.d.length() > 0)
			return true;
		else
			return false;
	}

	private Boolean isSourceFileInitialized()
	{
		if (this.s.length() > 0)
			return true;
		else
			return false;
	}
	
	private Boolean areFilesInitialized()
	{
		if ( isDestinationFileInitialized() && isSourceFileInitialized() )
			return true;
		else
			return false;
	}
}
